package com.resource.project.service.system.service.impl;


import com.resource.project.entity.system.UserDetail;
import com.resource.project.service.system.service.IHrService;
import com.resource.project.service.system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
@Transactional
public class UserService implements IUserService {

    @Resource
    private IHrService hrService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException,AuthenticationServiceException {
        UserDetail user = hrService.loadUserByUsername(s);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        return createUserDetailUser(user);
    }

    private UserDetails createUserDetailUser(UserDetail user) {
        List<GrantedAuthority> authorities = new ArrayList<>();
        return new User(user.getUsername(),user.getPassword(),authorities);
    }


}
